<?
    /**
     * Smarty plugin
     * @package Smarty
     * @subpackage plugins
     */


    /**
     * Smarty {load_listings} function plugin
     *
     * Type:     function
     * Name:     load_listings
     * Purpose:  Loads most recent listings from the database
     *   @param array
     *   @param Smarty
     *
     * This smarty plugin can be talked to using the following
     * template parameters:
     *
     *   * var (string)   -- name of variable where to load the
     *                       data to
     *                       >> default: listings
     *   * toget (string) -- total number of listing to get from
     *                       the database
     *                       >> default: 5
     *
     * @return string
     */
    function smarty_function_load_hotel_offers($params, &$smarty)
    {
        global $db; // create a link to the database connection

        //pre-process parameters
        //variable name
        if(empty($params['var'])){
            $var = 'articles';
        }else{
            $var = $params['var'];
        }
        
        if(!empty($params['where'])){
            $where = " AND (". $params['where'].") ";
        }else{
            $where = null;
        }

        //do we want to limit to some specific folder structure?
        if(empty($params['folder'])){
            $folder = null;
        }else{
            $node  = $db->getRow("SELECT * FROM structure WHERE id = ".(int)$params['folder']);
            $nodes = $db->getCol("SELECT id FROM structure WHERE lft >= ".(int)$node['lft']." AND rgt <= ".(int)$node['rgt']);
            if(!empty($nodes)){
                $folder = ' AND pm.nodeid IN('.implode(',',$nodes).') ';
            }
        }

        if(empty($params['image'])){
            $image = null;
        }else{
            $image = " AND ni.image = 't' ";
        }

        //how do I order by?
        if(empty($params['order'])){
            $order = 'pm.published';
        }else{
            $order = $params['order'];
        }

        //variable name
        if(empty($params['items'])){
            $total = 5;
        }else{
            $total = (int)$params['items'];
        }


        //pagetype
        $pageType = (int)$db->getOne("SELECT id FROM datatype WHERE lower(codeset) = 'hoteloffer'");

                                        
        $smarty->assign($var,$db->getAll("SELECT
                                            pm.created          AS intime,
                                            pm.title            AS title,
                                            ni.teaser           AS teaser,
                                            ni.teaser_title     AS teaser_title,
                                            ni.content          AS content,
                                            ni.hotel_title		AS hotel_title,
                                            ni.hotel_desc		AS hotel_desc,
                                            ni.service_desc		AS service_desc,
                                            ni.service_title	AS service_title,
                                            ni.image            AS image,
                                            ni.id               AS id,
                                            ni.price			AS price,
                                            ni.adr_1			AS adr_1,
                                            ni.adr_2			AS adr_2,
                                            ni.adr_city			AS adr_city,
                                            ni.adr_zip			AS adr_zip,
                                            ni.adr_country		AS adr_country,
                                            ni.gmaps_long		AS gmaps_long,
                                            ni.gmaps_lat		AS gmaps_lat,
                                            get_path(pm.nodeid)||pm.pageurl||'.html' AS url,
                                            'files/pages/".$pageType."__'||ni.id||'/' AS image_path
                                        FROM pagemap AS pm
                                        LEFT JOIN node_hotel_offer AS ni ON pm.pageid = ni.id
                                        WHERE pm.pagetype = " . $pageType . " " . $folder . " " . $image . " " . $splash . " " . $where . "
                                        AND pm.wf_state = 1
                                        ORDER BY ".$order." DESC LIMIT ".$total));
    }

